//
//  MusicHelper.swift
//  JiaoZiTaxi
//
//  Created by Hu Gang on 2022/11/25.
//

import Foundation
import UIKit
import AVFAudio

class AudioManager : ObservableObject {
    static let shared = AudioManager()

    private init() {
        logger.info("AudioManager inited.")
    }
    var player : AVAudioPlayer?

    static func playMp3(assetName:String) {
        if let asset = NSDataAsset(name:assetName){
            do {
                // Use NSDataAsset's data property to access the audio file stored in Sound.
                let player = try AVAudioPlayer(data:asset.data, fileTypeHint:"mp3")
                // Play the above sound file.
                player.play()
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    }

    func playNewMessage() {
        if let asset = NSDataAsset(name:"newmessage"){
            do {
                // Use NSDataAsset's data property to access the audio file stored in Sound.
                player = try AVAudioPlayer(data:asset.data, fileTypeHint:"mp3")
                // Play the above sound file.
                player!.play()
                print("play newmessage.")
            } catch let error as NSError {
                print(error.localizedDescription)
            }
        }
    }
}
